﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.App_Code;
using QXBook.Utils;
using System.Data;
using QXBook.Controls.HtmlControls;
using QXBook.Service;
using System.Web.UI.HtmlControls;

namespace Web.SysManage.Admin
{
    public partial class AdminRoleList : AdminBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //检测管理员登录
            ServiceFactory.CreateInstance<AdminService>().CheckAdminLogin();
            ServiceFactory.CreateInstance<AdminService>().CheckAdminPop(12);

            if (!Page.IsPostBack)
            {
                this.TextBind();
            }

            lmc.AddEditLinkUrl = "AdminRoleEdit.aspx";
            lmc.EnableLinkPublish = false;
            lmc.EnableLinkOn = false;
            lmc.EnableLinkOff = false;
            lmc.EnableLinkShipments = false;
            lmc.EnableLinkResave = false;

            lmc.EditHandler += new EventHandler(lmc_EditHandler);
            lmc.DeleteHandler += new EventHandler(lmc_DeleteHandler);
        }

        void lmc_EditHandler(object sender, EventArgs e)
        {
            int flags = 0;
            int memberID = 0;
            foreach (RepeaterItem item in rptList.Items)
            {
                HtmlInputCheckBox CheckID = (HtmlInputCheckBox)item.FindControl("CheckID");
                if (CheckID.Checked)
                {
                    //ServiceFactory.CreateInstance<AdminRuleService>().r
                    HtmlInputHidden hidID = (HtmlInputHidden)item.FindControl("hidID");
                    memberID = CommonUtils.ToInt(hidID.Value);
                    flags++;
                }
            }

            if (flags == 1)
            {
                Response.Redirect("AdminRoleEdit.aspx?RoleID=" + memberID);
            }
            else if (flags > 1)
            {
                base.ResponseErch("只能选择一个");
            }
            else
            {
                base.ResponseErch("请选择一个角色组");
            }
        }

        void lmc_DeleteHandler(object sender, EventArgs e)
        {
            int flags = 0;
            int ruleID = 0;
            foreach (RepeaterItem item in rptList.Items)
            {
                HtmlInputCheckBox CheckID = (HtmlInputCheckBox)item.FindControl("CheckID");
                if (CheckID.Checked)
                {
                    //ServiceFactory.CreateInstance<AdminRuleService>().r
                    HtmlInputHidden hidID = (HtmlInputHidden)item.FindControl("hidID");
                    ruleID += CommonUtils.ToInt(hidID.Value);
                    flags++;
                }
            }

            if (flags == 1)
            {
                ServiceFactory.CreateInstance<AdminRuleService>().DelAdminRole(base.adminInfo, ruleID);
                base.ResponseErch("删除成功");
                this.TextBind();
            }
            else if (flags > 1)
            {
                base.ResponseErch("只能选择一个");
            }
            else
            {
                base.ResponseErch("请选择一个角色组");
            }
        }

        protected void TextBind()
        {
            DataSet ds = new DataSet();
            string strRolename = string.Empty;
            int rolecount = 0;

            int rtn = ServiceFactory.CreateInstance<AdminRuleService>().GetAdminRoleInfo(ref rolecount, ref ds);
            if (rtn < 0)
            {
                return;
            }

            this.sRolecount.Text = rolecount.ToString();
            this.rptList.DataSource = ds;
            this.rptList.DataBind();
        }

        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataRowView Row = (DataRowView)e.Item.DataItem;
            Anchor aRoleName = (Anchor)e.Item.FindControl("aRoleName");
            Anchor aNum = (Anchor)e.Item.FindControl("aNum");
            Span sUpdateDate = (Span)e.Item.FindControl("sUpdateDate");

            aRoleName.Text = Row["RoleName"].ToString();
            aRoleName.Href = "AdminRoleEdit.aspx?RoleID=" + CommonUtils.UrlEncode(Row["RoleID"].ToString());

            aNum.Text = Row["Num"].ToString();
            aNum.Href = "AdminList.aspx?RoleID=" + CommonUtils.UrlEncode(Row["RoleID"].ToString());

            sUpdateDate.Text = !string.IsNullOrEmpty(CommonUtils.ToString(Row["UpdateDate"])) ?
                ((DateTime)Row["UpdateDate"]).ToString("yyyy-MM-dd")
                : string.Empty;
        }
    }
}